package com.mcintyret.site.web.controller.journal;

import com.mcintyret.site.journal.database.JournalDao;
import com.mcintyret.site.web.controller.BaseController;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
 * User: mcintyret2
 * Date: 06/04/2012
 */
public abstract class BaseJournalController extends BaseController {

    @Resource
    protected JournalDao journalDao;

    @InitBinder
    public void binder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    setValue(new Date(Long.parseLong(value)));
                } catch (NumberFormatException e) {
                    setValue(null);
                }
            }

            public String getAsText() {
                return Long.toString(((Date)getValue()).getTime());
            }

        });
    }
}
